import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_module/config/ServiceUlr.dart';
import 'package:flutter_module/view_model/query_wuliao.dart';
import 'package:flutter_module/widget/provider_widget.dart';
import 'package:flutter_module/widget/tap_appbar.dart';
import 'package:flutter_screenutil/screenutil.dart';

class TuiHuiJiLu extends StatefulWidget {
  @override
  _TuiHuiJiLu createState() => _TuiHuiJiLu();
}

class _TuiHuiJiLu extends State<TuiHuiJiLu> {
  List<List> list = [];
  var map = {
    "1": "交通维护类",
    "2": "水管接头",
    "3": "水泵控制柜",
    "4": "工作服",
    "5": "安全救生",
    "6": "小工具",
    "7": "排水专用工具",
    "8": "其他",
    "9": "全部",
    "1001": "讯腾物料1",
    "1002": "讯腾物料2"
  };

  var map1 = {
    "materielType": "",
    "materielNo": "",
    "materielName": "",
    "pageIndex": "1",
    "pageSize": "10",
    "companyId": "402880185889ef6e015889f8c51e0000",
    "userId": "8a2a2a295c1ec767015c2ddd9d1e0001",
  };

  String value;

  String stockReturnNo;
  String stockOutputNo;
  var sDate = " ";
  var eDat = "";
  wuliaoQueryModel queryModel = new wuliaoQueryModel();

  @override
  void initState() {
    // TODO: implement initState
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
//      backgroundColor:Colors.white,
      appBar: TapAppBar(TapViewBar(Text("退回记录"), icon: Icon(Icons.arrow_back)),
          Size.fromHeight(ScreenUtil().setHeight(80))),
      body: ProviderWidget<wuliaoQueryModel>(
        model: wuliaoQueryModel(),
        onModelInit: (model) {
          model.url = "queryMateriels";
          model
              .getHttpData(servicePath["stockReturnRecord"], data: map1)
              .then((value) {
            list = model.data;
          });
        },
        builder: (context, model, child) {
          return EasyRefresh(
              controller: model.controller,
              footer: MaterialFooter(),
              enableControlFinishLoad: true,
              onLoad: model.onLoad,
              bottomBouncing: true,
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    getInPutName1(),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getInPutName(),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    Container(
                      color: Colors.white,
                      child: getDate2("开始时间"),
                    ),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    Container(
                      color: Colors.white,
                      child: getDate2("结束时间"),
                    ),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getQuery(model),
                    getWrap(),
                  ],
                ),
              ));
        },
      ),
    );
  }

  List<Widget> getListItem() {
    return list.map((value) {
      return Container(
        margin: EdgeInsets.only(left: 10, right: 10, top: 10),
        padding: EdgeInsets.only(left: 20, top: 10),
        decoration: new BoxDecoration(
          //背景
          color: Colors.white,
          //设置四周圆角 角度
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
          //设置四周边框
//          border: new Border.all(width: 1, color: Colors.red),
        ),
//        color: Colors.white,
        child: Column(
          children: <Widget>[
            getListItemData("出库单号:", value[0]),
            SizedBox(
              height: 10,
            ),
            getListItemData("退回单号:", value[1]),
            SizedBox(
              height: 10,
            ),
            getListItemData("退回日期:", value[2]),
            SizedBox(
              height: 10,
            ),
            getListItemData("类        别:", value[3]),
            SizedBox(
              height: 10,
            ),
            getListItemData("名        称:", value[4]),
            SizedBox(
              height: 10,
            ),
            getListItemData("规        格:", value[5]),
            SizedBox(
              height: 10,
            ),
            getListItemData("计量单位:", value[6]),
            SizedBox(
              height: 10,
            ),
            getListItemData("退回原因:", value[7]),
            SizedBox(
              height: 10,
            ),
            getListItemData("数        量:", value[8]),
            SizedBox(
              height: 10,
            ),
            getListItemData("存库名称:", value[9]),
            SizedBox(
              height: 10,
            ),
            getListItemData("备        注:", value[10]),
            SizedBox(
              height: 10,
            ),
          ],
        ),
      );
    }).toList();
  }

  Widget getListItemData(String name, String data) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Text(
            name,
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          flex: 1,
        ),
        Expanded(
          child: Text(
            data,
            style: TextStyle(
                color: name == "状        态:" ? Colors.red : Colors.black54),
          ),
          flex: 4,
        )
      ],
    );
  }
  Widget getInPutName() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text("退回单号"),
            flex: 1,
          ),
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                //  labelText: sp.getUserInfo("num").toString(),
                fillColor: Colors.white,
                filled: true,
                border: InputBorder.none,
                hintText: "",
              ),
              onChanged: (value) {
                stockReturnNo = value;
              },
            ),
            flex: 2,
          )
        ],
      ),
    );
  }

  Widget getInPutName1() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text("出库单号"),
            flex: 1,
          ),
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                //  labelText: sp.getUserInfo("num").toString(),
                fillColor: Colors.white,
                filled: true,
                border: InputBorder.none,
                hintText: "",
              ),
              onChanged: (value) {
                stockOutputNo = value;
              },
            ),
            flex: 2,
          )
        ],
      ),
    );
  }

  Widget getQuery(wuliaoQueryModel model) {
    return Container(
      // color: Colors.black12,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      margin: EdgeInsets.only(top: 10),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: RaisedButton(
              onPressed: () {
//                stockOutputNo:
//                stockReturnNo:
//                minStockReturnDate:
//                maxStockReturnDate:
                var map = {
                  "stockOutputNo": this.stockOutputNo,
                  "minStockOutputDate": this.sDate,
                  "maxStockOutputDate": this.eDat,
                  "stockReturnNo":this.stockReturnNo,
                  "companyId": "402880185889ef6e015889f8c51e0000",
                  "userId": "8a2a2a295c1ec767015c2ddd9d1e0001",
                };
                model.map = map;
                model.query = false;
                model.getHttpData(servicePath["stockReturnRecord"], data: map);

              },
              child: Text("查询"),
              textColor: Colors.white,
              color: Theme.of(context).primaryColor,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
                // side: BorderSide(color: Colors.red)
              ),
            ),
            flex: 1,
          ),
//          Text("   "),
        ],
      ),
    );
  }

  Widget getWrap() {
    if (list == null) {
      return Text(" ");
    } else {
      return Container(
//        padding: EdgeInsets.only(left: 30),
        child: Wrap(
          spacing: 1,
          children: getListItem(),
        ),
      );
    }
  }

  Widget getDate(String sDate) {
    return Container(
      height: ScreenUtil().setHeight(80),
      child: FlatButton(
          onPressed: () {
            DatePicker.showDatePicker(context,
                showTitleActions: true,
                minTime: DateTime(2010, 3, 5),
                maxTime: DateTime(2030, 6, 7),
                onChanged: (date) {}, onConfirm: (date) {
                  setState(() {
                    this.sDate = date.toString().substring(0, 10);
                  });
                }, currentTime: DateTime.now(), locale: LocaleType.zh);
          },
          child: Container(
            alignment: Alignment.center,
            color: Colors.white,

            padding: EdgeInsets.only(left: 15, right: 30),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Text(sDate),
                  flex: 1,
                ),
                Expanded(
                  child: Text(this.sDate),
                  flex: 2,
                )
              ],
            ),
          )),
    );
  }

  Widget getDate2(String eDate) {
    return Container(
      height: ScreenUtil().setHeight(80),
      child: FlatButton(
          onPressed: () {
            DatePicker.showDatePicker(context,
                showTitleActions: true,
                minTime: DateTime(2010, 3, 5),
                maxTime: DateTime(2030, 6, 7),
                onChanged: (date) {}, onConfirm: (date) {
                  setState(() {
                    this.eDat = date.toString().substring(0, 10);
                  });
                }, currentTime: DateTime.now(), locale: LocaleType.zh);
          },
          child: Container(
            alignment: Alignment.center,
            color: Colors.white,

            padding: EdgeInsets.only(left: 15, right: 30),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Text(eDate),
                  flex: 1,
                ),
                Expanded(
                  child: Text(this.eDat),
                  flex: 2,
                )
              ],
            ),
          )),
    );
  }
}
